﻿#region Copyright
// Copyright (c) 2011-2012 RaisingForce Team (Alikin Sergey)
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Diagnostics.Contracts;
using RaisingForce.Raise.Hardware;
using RaisingForce.Raise.Presentation.Localization;

namespace RaisingForce.RaiseHardwareTest.ViewModel.MainStates
{
    sealed internal class MainViewModelData :
        ILocalizable
    {
        #region Constructors

        public MainViewModelData(Func<MainStatePool> statePoolCreator)
        {
            Contract.Requires(statePoolCreator != null);
            this.statePoolCreator = statePoolCreator;
            this.updateStatePool();
        }

        #endregion

        #region Private Declarations

        private readonly Func<MainStatePool> statePoolCreator;

        private MainStatePool statePool;

        private void updateStatePool()
        {
            this.statePool = this.statePoolCreator();
        }

        private readonly object syncObject = new object();

        private string currentStateKey; // = null

        private void updateState()
        {
            this.State = this.currentStateKey != null ?
                this.statePool.GetState(this.currentStateKey) :
                null;
        }

        [ContractInvariantMethod]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "Required for code contracts.")]
        private void ObjectInvariant()
        {
            Contract.Invariant(this.statePool != null);
        }

        #endregion

        #region Properties

        public IRobotHardware Hardware { get; set; }

        public bool HardwareNeedsReinit { get; set; }

        public IMainState State { get; private set; }

        #endregion

        #region StateChanged Event Handler

        public event EventHandler StateChanged;

        private void OnStateChanged()
        {
            if (this.StateChanged != null)
            {
                this.StateChanged(this, EventArgs.Empty);
            }
        }

        #endregion

        #region Public Methods

        public void SetState(string stateKey)
        {
            bool stateChanged = false;
            lock (this.syncObject)
            {
                if (this.currentStateKey != stateKey
                    && this.statePool.ContainsState(stateKey))
                {
                    this.currentStateKey = stateKey;
                    this.updateState();
                    stateChanged = true;
                }
            }
            if (stateChanged)
            {
                this.OnStateChanged();
            }
        }

        public void ResetHardware()
        {
            lock (this.syncObject)
            {
                if (this.Hardware != null)
                {
                    this.HardwareNeedsReinit = true;
                }
            }
        }

        #endregion

        #region ILocalizable Members

        public void UpdateLocalization()
        {
            this.updateStatePool();
            this.updateState();
        }

        #endregion
    }
}
